<?php namespace App\Http\Services\Weibo;

use Requests;
use Requests_Exception;
use Log;

class WeiboLogin
{
    private $weibo_oauth2_url = 'https://api.weibo.com/oauth2/authorize';

    public static function buildRedirectUrl($callback = null){
        $config = new config();
        $weiboLogin = new WeiboLogin();
        $appid  = $config->getAppid();
        $appkey = $config->getSecret();

        $getCodeUrl = $weiboLogin->weibo_oauth2_url.'?client_id='.$appid.'&redirect_uri='.$callback.'&response_type=code';

        return $getCodeUrl;
    }

    public static function getAccessToken($callback,$code){
        $config = new config();
        $weiboLogin = new WeiboLogin();
        $appid  = $config->getAppid();
        $appkey = $config->getSecret();

        $body = array(
            'client_id'     =>$appid,
            'client_secret' =>$appkey,
            'grant_type'    => 'authorization_code',
            ''
        );

//        return $getCodeUrl;
    }


    public static function doPost($url, array $param)
    {
        $response = null;
        try {
            $response = Requests::post($url, [
                'Content-Type' => 'application/json;charset=utf-8',
            ], $param);
        } catch (Requests_Exception $ex) {
            Log::error('Request Error To YunPian:'.$ex);
        }

        if (!$response->success) {
            return false;
        }

        $res = json_decode($response->body);
        if (is_null($res))
            return false;

        return $res;
    }
}